@extends('layouts.admin')

@section('content')
<div class="page-header">
	<h2>Template - 
		<small>
		@if($method == 'post')
			Add a new template
		@elseif($method == 'delete')
			Delete {{$template->name}} template ?
		@else
			Edit {{$template->name}} template
		@endif
		</small>
	</h2>
</div>
@foreach ($errors->all() as $message)
<div class="row">
	<div class="col-lg-12">
		<div class="alert alert-danger alert-block">
			<button type="button" class="close" data-dismiss="alert">&times;</button>
			<h4>Unsuccess</h4>
			{{{ $message }}}
		</div>
	</div>
</div>
@endforeach

<div class="row">
	<div class="col-lg-12">
		{{Form::model($template, array('method' => $method, 'url'=>'templates/'.$template->id, 'class' => 'form-horizontal'))}}
		@unless($method == 'delete')
			<!-- Template Name -->
			<div class="form-group {{{ $errors->has('template_name') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('template_name', 'Template Name', array('class' => 'control-label')) }}
					{{ Form::text('template_name', Input::old('template_name'), array('class' => 'form-control')) }}
				</div>
			</div>
			
			<div class="form-group">
				<div class="col-sm-4">
					<label>Template params <small><button id="add_field_button" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span></button></small></label>
				</div>
			</div>
			
			<div id="input_fields_wrap">
			@if (count(Input::old('template_param_name')) > 0)
				@foreach (Input::old('template_param_name') as $key => $template_param_name)
					<div class="form-group">
						<div class="col-sm-4">
							<div class="input-group">
								<input type="text" name="template_param_name[]" class="form-control" value="{{ $template_param_name }}">
								<span class="remove_field input-group-addon" id="basic-addon2"><span class="glyphicon glyphicon-remove"></span></span>
							</div>
						</div>
					</div>
				@endforeach
			@elseif (count($template->templateParams) > 0)
				@foreach ($template->templateParams as $key => $template_param_name)
					<div class="form-group">
						<div class="col-sm-4">
							<div class="input-group">
								<input type="text" name="template_param_name[]" class="form-control" value="{{ $template_param_name->template_param_name }}">
								<span class="remove_field input-group-addon" id="basic-addon2"><span class="glyphicon glyphicon-remove"></span></span>
							</div>
						</div>
					</div>
				@endforeach
			@endif
			</div>
			
			<div class="form-group">
				<div class="col-sm-4">
					{{Form::submit("Save", array("class"=>"btn btn-success"))}}
					<a href="{{url('templates')}}" class="btn btn-default">
						Cancel
					</a>
				</div>
			</div>
		@else
			<div class="form-group">
				<div class="col-sm-4">
					{{Form::submit("Delete", array("class"=>"btn btn-success"))}}
					<a href="{{url('templates')}}" class="btn btn-default">
						Cancel
					</a>
				</div>
			</div>
		@endif
		{{Form::close()}}
	</div>
</div>
@stop

@section('endscript')
@parent
<script>
$(document).ready(function() {
    var wrapper         = $("#input_fields_wrap"); //Fields wrapper
    var add_button      = $("#add_field_button"); //Add button ID
    
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        $(wrapper).append('<div class="form-group"><div class="col-sm-4"><div class="input-group"><input type="text" name="template_param_name[]" class="form-control"><span class="remove_field input-group-addon" id="basic-addon2"><span class="glyphicon glyphicon-remove"></span></span></div></div></div>'); //add input box
    });
    
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').parent('div').parent('div').remove();
    })
});
</script>
@stop
